package ui.panel.common
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	import globals.ButtonType;
	import globals.Global;

	import net.data.HeroInfo;

	import ui.component.Page;
	import ui.component.ScaleBitmap;
	import ui.component.TextButton;

	import util.DisplayUtil;
	import util.Long;
	import util.UIUtil;

	/**
	 * (弹出的)英雄列表
	 * @author luguobin
	 */
	public class HeroList extends Sprite
	{
		private var _page:Page;
		private var _items:Array;
		private var _data:Vector.<HeroInfo>;

		public function HeroList()
		{
			_items = [];
			addBg();
		}

		private function addBg():void
		{
			var bg:ScaleBitmap = new ScaleBitmap(Global.assetsManager.getAssetsAsBitmapData("float_content_bg"));
			bg.scale9Grid = new Rectangle(35, 35, 18, 18);
			bg.width = 288;
			bg.height = 346;
			this.addChild(bg);

			for(var i:int = 0; i < 16; i++)
			{
				var item:TextButton = UIUtil.getTextButton(i, '', ButtonType.BUTTON_LIST, btnHandle);
				item.x = 15 + int(i / 8) * 130;
				item.y = 20 + i % 8 * 35;
				_items.push(item);
				this.addChild(item);
			}

			_page = new Page(pageHandler);
			_page.x = 78;
			_page.y = 310;
			this.addChild(_page);
		}

		private function pageHandler(num:uint):void
		{
			for(var i:int = 0; i < 16; i++)
			{
				var curIndex:int = (num - 1) * 16 + i;
				var item:TextButton = _items[i];

				if(curIndex < _data.length)
				{
					item.enable = true;
					item.text = "好色无敌猫 lv." + i;
				}
				else
				{
					item.enable = false;
					item.text = "";
				}
			}
		}

		private function btnHandle(btn:TextButton):void
		{
			if(_selectCallback != null)
			{
				var index:int = (_page.curNum - 1) * 16 + btn.id;
				var curHeroId:Long = (_data[index] as HeroInfo).heroId;
				_selectCallback(curHeroId);
			}
		}

		public function update(data:Vector.<HeroInfo> = null):void
		{
			_data = data;
			var len:int = data.length;
			_page.max = int(len / 16) + (len % 16 > 0 ? 1 : 0);
			pageHandler(1);
		}

		private var _selectCallback:Function;

		public function set selectCallback(value:Function):void
		{
			_selectCallback = value;
		}
	}
}